package com.demo.service.impl;

import com.demo.common.base.ResultModel;
import com.demo.common.exception.BusinessException;
import com.demo.dao.FeedbackMapper;
import com.demo.model.Feedback;
import com.demo.model.User;
import com.demo.service.FeedbackService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: FeedbackServiceImpl
 * @Author: ChenXueLi
 * @Date: 2018-10-05 22:45
 */
@Slf4j
@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    /**
     * 添加反馈信息
     * @param feedback
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel addFeedback(Feedback feedback, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
        feedback.initModel();
        int effectRows = feedbackMapper.addFeedback(feedback);
        resultModel.info();
        return resultModel;
    }

    /**
     * 删除反馈信息
     * @param feedback
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel deleteFeedback(Feedback feedback, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
        feedback = feedbackMapper.getFeedbackById(feedback.getFeedbackId());
        if(feedback == null) {
            throw new BusinessException(-101, "反馈信息查询失败");
        }

        feedback.modify(resultModel.getUserInfo());

        int effectRows = feedbackMapper.deleteFeedback(feedback);

        resultModel.info("删除成功");
        return resultModel;
    }

    /**
     * 更新反馈信息
     * @param feedback
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel updateFeedback(Feedback feedback, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
        return resultModel;
    }

    /**
     * 根据id查询反馈信息
     * @param feedback
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel getFeedbackById(Feedback feedback, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {
        feedback = feedbackMapper.getFeedbackById(feedback.getFeedbackId());
        if(feedback == null) {
            throw new BusinessException(-101, "反馈信息查询失败");
        }
        resultModel.info("查询成功");
        resultModel.setData(feedback);
        return resultModel;
    }

    /**
     * 查询反馈信息列表
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel getFeedbackList(ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {

        PageHelper.startPage(resultModel.getPage(),  resultModel.getRows());

        List<Feedback> feedbackList = feedbackMapper.getFeedbackList(null);
        PageInfo<Feedback> pageInfo = new PageInfo<>(feedbackList);
        resultModel.setData(pageInfo);

        resultModel.info("查询成功");
        return resultModel;
    }

    /**
     * 快速修改反馈信息处理状态
     * @param feedback
     * @param resultModel
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public ResultModel modifyDealState(Feedback feedback, ResultModel resultModel, HttpServletRequest request, HttpServletResponse response) throws Exception {

        int state = feedback.getFeedbackIsDeal();

        feedback = feedbackMapper.getFeedbackById(feedback.getFeedbackId());
        if(feedback == null) {
            throw new BusinessException(-101, "反馈信息查询失败");
        }

        feedback.setFeedbackIsDeal(state);
        feedback.modify(resultModel.getUserInfo());

        int effectRows = feedbackMapper.updateFeedback(feedback);

        resultModel.info();
        return resultModel;
    }
}
